import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

import '../../generated/l10n.dart';
import 'chart_bar_widget.dart';
import 'chart_time_option_utils.dart';
import 'model/chart_dot_data.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/theme/gt_colors_v3.dart';

/// @Title   没有数据的图表页面
/// @Author: bryce
/// @CreateDate:  2023/8/17 16:32
class ChartEmptyWidget extends StatefulWidget {
  /// 过去几天的数据（即使是空数据，在横坐标也要显示日期，这里传入从现在算起向前过去了多少天的数据，和下面的timeOption二选一）
  final int? lastDays;

  /// 过去多少天和枚举类，和上面参数二选一传入(已过时，使用)
  @Deprecated("instead of lastDays or startTime/endTime")
  final ChartTimeOption? timeOption;
  final double height;

  /// x轴所有显示的文案（如果传了这个参数，则不用传上面的lastDays和timeOption）
  final List<String>? bottomTitles;

  /// 是否仅展示x轴的收尾坐标
  final bool onlyShowFirstLastBottomTitle;

  /// x轴的固定间隔(如果不传会自动计算，传入的话需要注意间隔太小的话会放不下)
  final int? xInterval;

  final Widget? centerWidget;

  const ChartEmptyWidget({
    super.key,
    this.timeOption,
    this.lastDays,
    this.bottomTitles,
    this.height = 200,
    this.onlyShowFirstLastBottomTitle = false,
    this.xInterval,
    this.centerWidget,
  });

  @override
  State<StatefulWidget> createState() => _ChartEmptyWidgetState();
}

class _ChartEmptyWidgetState extends State<ChartEmptyWidget> {
  List<ChartDotData> dots = [];

  @override
  void initState() {
    super.initState();
    _initDotData();
  }

  @override
  void didUpdateWidget(covariant ChartEmptyWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    _initDotData();
  }

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    return SizedBox(
      key: ValueKey(widget.bottomTitles),
      width: width,
      child: Stack(
        alignment: Alignment.center,
        children: [
          SizedBox(
            width: width,
            child: ChartBarWidget(
              key: ValueKey(dots.length),
              dots: dots,
              height: widget.height,
              rightPadding: 0,
              topColor: getColorByValue(1),
              bottomColor: getColorByValue(-1),
              minY: 100,
              maxY: 200,
              enableTouch: false,
              onlyShowFirstLastBottomTitle: widget.onlyShowFirstLastBottomTitle,
              xInterval: widget.xInterval,
              getLeftTitleByY: (y) => '--',
              chartRowHeight: 32,
            ),
          ),
          Padding(
            padding: GTEdgeInsetsOnly(bottom: 30),
            child: widget.centerWidget ??
                Text(
                  LibUIKitS.current.no_data,
                  style: TextStyle(color: GTColorsV3.uikitText3V3.color, fontSize: 14),
                ),
          )
        ],
      ),
    );
  }

  List<ChartDotData> _initDotData() {
    dots.clear();
    if (null != widget.bottomTitles) {
      dots = widget.bottomTitles!.map((e) => ChartDotData(e, 0)).toList();
    } else {
      int dateCount = widget.lastDays ?? ChartTimeOptionUtils.getDays(widget.timeOption);
      DateTime today = DateTime.now();
      dots.add(ChartDotData(DateFormat('MM-dd').format(today), 0));
      for (int i = 0; i < dateCount; i++) {
        DateTime date = today.subtract(Duration(days: i + 1));
        dots.add(ChartDotData(DateFormat('MM-dd').format(date), 0));
      }
      dots = dots.reversed.toList();
    }

    return dots;
  }

  /// 根据盈亏值判断获取显示色
  static Color getColorByValue(double value) {
    // 是否是正数
    bool isPositive = value >= 0;
    bool isHzld = GTThemeProvider.instance.isHzld();
    if (isHzld) {
      return isPositive ? GTColorsV3.uikitPd1V3.color : GTColorsV3.uikitPd2V3.color;
    }
    return isPositive ? GTColorsV3.uikitPd2V3.color : GTColorsV3.uikitPd1V3.color;
  }
}
